package com.travel.server.manager.impl.command.adversiting;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.advertising.UpdateAdvertisingSourceRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.SourceDal;
import com.travel.server.manager.model.AdvertisingSourceInfo;
import com.travel.server.manager.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/20 15:45
 * @Desc:
 */
@Component
public class UpdateAdvertisingSourceCommand extends AbstractCommand<UpdateAdvertisingSourceRequest, EmptyResponse> {

    @Autowired
    private SourceDal sourceDal;

    @Override
    protected void checkInput(UpdateAdvertisingSourceRequest request) {

    }

    @Override
    public EmptyResponse handle(UpdateAdvertisingSourceRequest request) {
        UserInfo userInfo = getUserInfo();
        Date now = new Date();

        AdvertisingSourceInfo info;
        if(request.getId() == null){
            info = new AdvertisingSourceInfo();
            info.setCreateBy(userInfo.getName());
            info.setCreateTime(now);
            info.setState(CommonConstant.YES);
        }else{
            info = sourceDal.selectById(request.getId());
        }
        info.setHyperLink(request.getHyperLink());
        info.setPositionId(request.getPositionId());
        info.setSort(request.getSort());
        info.setSourceTitle(request.getTitle());
        info.setSourceType(request.getType());
        info.setUpdateBy(userInfo.getName());
        info.setUpdateTime(now);
        info.setUrl(request.getUrl());
        sourceDal.insertOrUpdate(info);
        return new EmptyResponse();
    }
}
